<template>
	<view class="main-content">
		<MHeader>{{state.pageName}}</MHeader>
		<scroll-view scroll-y="true" style="height:calc(100% - 80rpx)">
			<!-- 业务员 财务补充信息时不能修改业务员填写的内容 -->
			<view>
				<item name="单据编号">
					<view class="input">{{state.info.Data.Model.BillNo}}</view>
				</item>
				<item name="单据状态">
					<view class="input">{{state.info.Data.Model.DocumentStatus?state.status[state.info.Data.Model.DocumentStatus]:''}}</view>
				</item>
				<item name="上苗状态">
					<view class="input">{{state.smStatus[state.info.Data.Model.F_QUQY_SMSTATE]}}</view>
				</item>
				<item name="组织">
					<view>{{state.info.Data.Model.F_QUQY_ORGName}}</view>
				</item>
				<item name="养殖户">
					<view>{{state.info.Data.Model.F_QUQY_FARMERSName}}</view>
				</item>
				<item name="在栏数量">
					<view style="padding-right: 35rpx;">{{state.info.Data.Model.F_QUQY_COLUMNQTY}}</view>
					<text class="symbol">只</text>
				</item>
				<item name="苗损数量">
					<view style="padding-right: 35rpx;">{{state.info.Data.Model.F_QUQY_LOSSQTY||0}}</view>
					<text class="symbol">只</text>
				</item>
				<item name="上苗日期">
					<view>{{state.info.Data.Model.F_QUQY_SEEDDATE}}</view>
				</item>
				<item name="合同苗数量">
					<view style="padding-right: 35rpx;">{{state.info.Data.Model.F_QUQY_HTMQTY}}</view>
					<text class="symbol">只</text>
				</item>
				<item name="市场苗数量">
					<view style="padding-right: 35rpx;">{{state.info.Data.Model.F_QUQY_SCMQTY}}</view>
					<text class="symbol">只</text>
				</item>
				<item name="上苗总数量">
					<view style="padding-right: 35rpx;">{{state.info.Data.Model.F_QUQY_QTY}}</view>
					<text class="symbol">只</text>
				</item>
				<item name="合同苗价">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_HTMPRICE}}</view>
					<text class="symbol">元 / 只</text>
				</item>
				<item name="回收价格">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_HSPRICE}}</view>
					<text class="symbol">元 / 斤</text>
				</item>
				<item name="饲料价格">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_SLPRICE}}</view>
					<text class="symbol">元 / 袋</text>
				</item>
				<item name="联系电话">
					<view type="number">{{state.info.Data.Model.F_QUQY_PHONE}}</view>
				</item>
				<item name="养殖类型">
					<view>{{state.info.Data.Model.TypeName}}</view>
				</item>
				<item name="里程(KM)">
					<view style="padding-right: 45rpx;">{{state.info.Data.Model.F_QUQY_MILEAGE}}</view>
					<text class="symbol">KM</text>
				</item>
				<item name="业务员">
					<view>{{state.info.Data.Model.F_QUQY_YWYName}}</view>
				</item>
				<item name="送料车辆">
					<view>{{state.info.Data.Model.F_QUQY_LIAOCARName}}</view>
				</item>
				<item name="送料车牌号">
					<view>{{state.info.Data.Model.F_QUQY_LIAOCARTEM}}</view>
				</item>
				<item name="养户地址">
					<view>{{state.info.Data.Model.F_QUQY_ADDRESS}}</view>
				</item>
				<item name="组织客户">
					<view>{{state.info.Data.Model.F_QUQY_ORGCUSTName}}</view>
				</item>
				<item name="备注">
					<view>{{state.info.Data.Model.F_QUQY_REMARK}}</view>
				</item>
				<item name="屠宰场">
					<view>{{state.info.Data.Model.F_QUQY_ABATTOIRName}}</view>
				</item>
			</view>
			<view>
				<UpLoad ref="upload" disabled></UpLoad>
			</view>
			<view class="gap"></view>
			<!-- 养殖公司财务专员 -->
			<view v-if="state.myInfo.role.includes(7)">

				<item name="采购苗价">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_MIAOPRICE}}</view>
					<text class="symbol">元 / 只</text>
				</item>
				<item name="保证金">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_BZJPRICE}}</view>
					<text class="symbol">元 / 只</text>
				</item>
				<item name="风险金">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_FXPRICE}}</view>
					<text class="symbol">元 / 只</text>
				</item>
				<item name="兽药只价">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_SYPRICE}}</view>
					<text class="symbol">元 / 只</text>
				</item>
	<!-- 			<item name="饲料价格">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_SLPRICE}}</view>
					<text class="symbol">元 / 袋</text>
				</item> -->
				<item name="标准用料">
					<view style="padding-right: 85rpx;">{{state.info.Data.Model.F_QUQY_STANDARD}}</view>
					<text class="symbol">斤 / 只</text>
				</item>
				<item name="苗供应商">
					<view>{{state.info.Data.Model.F_QUQY_SUPPLIERName}}</view>
				</item>
				<item name="送苗车辆">
					<view>{{state.info.Data.Model.F_QUQY_MIAOCARName}}</view>
				</item>
				<item name="送苗车牌号">
					<view>{{state.info.Data.Model.F_QUQY_MIAOCARTEM}}</view>
				</item>
				<item name="技术员">
					<view>{{state.info.Data.Model.F_QUQY_JSYName}}</view>
				</item>
				<item name="屠宰保价">
					<view>{{state.info.Data.Model.F_QUQY_TZBJ}}</view>
				</item>
				<item name="经销模式">
					<view>{{state.F_QUQY_JXTYPEList[state.info.Data.Model.F_QUQY_JXTYPE]}}</view>
				</item>
				<item name="经销商" v-if="state.info.Data.Model.F_QUQY_JXTYPE==2">
					<view>{{state.info.Data.Model.F_QUQY_JXSName}}</view>
				</item>
<!-- 				<MHeader hidTag>
					<van-icon name="cluster" color="#1989fa" style="margin-right: 10rpx" />
					明细
				</MHeader>
				<view style="padding-bottom: 10rpx">
					<view class="card" v-for="(item, index) in state.info.Data.Model.FEntity">
						<view class="card-index">{{ index + 1 }}</view>
						<item name="料">
							<view>{{item.F_QUQY_MATERIALName}}</view>
						</item>

						<item name="合同价">
							<view style="padding-right: 35rpx;">{{item.F_QUQY_HTPRICE}}</view>
							<text class="symbol">元</text>
						</item>
					</view>
				</view> -->
			</view>
		</scroll-view>
	</view>
	<Loading v-if="state.loading"></Loading>
</template>

<script setup>
	// 业务员：新增（保存）提交 撤销   财务：审核 反审核
	// QUQY_SeedPlan
	import moment from 'moment'
	import { reactive, getCurrentInstance, ref, watch, nextTick } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { checkForm, msg } from '/utils/utils.js'
	import kdApis from '/api/modularApi/kdApi.js'
	const { proxy } = getCurrentInstance()
	const state = reactive({
		info: {
			FormId: 'QUQY_SeedPlan',
			Data: {
				Model: {
					F_QUQY_ORGID: {
						FNumber: null
					},
					F_QUQY_FARMERS: {
						FNumber: null
					},
					F_QUQY_YWY: {
						FSTAFFNUMBER: null
					},
					F_QUQY_LIAOCAR: {
						FNUMBER: null
					},
					F_QUQY_ORGCUST: {
						FNUMBER: null
					},
					TypeName: null
				}
			}
		},
		// 财务需要填写的
		Model2: {
			F_QUQY_ABATTOIR: {
				FNUMBER: null
			},
			F_QUQY_SUPPLIER: {
				FNUMBER: null
			},
			F_QUQY_MIAOCAR: {
				FNUMBER: null
			},
			F_QUQY_JSY: {
				FSTAFFNUMBER: null
			},
		},
		pageName: "上苗计划详情",
		myInfo: null,
		yzType: {
			0: '全架',
			1: '半架',
			2: '地养',
			3: '封闭棚'
		},
		status: {
			Z: '暂 存',
			A: '保 存',
			B: '审核中',
			C: '已审核',
			D: '重新审核'
		},
		// 上苗状态
		smStatus: {
			A: '申请中',
			B: '上苗中',
			C: '部分结算',
			D: '完全结算',
			E: '无',
			F: '重新上苗'
		},
		loading: true,
		F_QUQY_JXTYPEList:{
			1:'普通模式',
			2:'经销商模式'
		}
	})

	const getDetails = (Id) => {
		kdApis.formView({
			formId: 'QUQY_SeedPlan',
			data: { Id }
		}).then(res => {
			if (res) {
				let obj = {
					BillNo: res.BillNo,
					FId: res.Id,
					F_QUQY_SMSTATE: res.F_QUQY_SMSTATE,
					F_QUQY_ORGID: {
						FNumber: res.F_QUQY_ORGID?.Number
					},
					F_QUQY_ORGName: res.F_QUQY_ORGID?.Name[0].Value,
					F_QUQY_FARMERS: {
						FNumber: res.F_QUQY_FARMERS?.Number
					},
					F_QUQY_FARMERSName: res.F_QUQY_FARMERS?.Name[0].Value,
					F_QUQY_SEEDDATE: moment(res.F_QUQY_SEEDDATE).format('YYYY-MM-DD'),
					F_QUQY_QTY: res.F_QUQY_QTY,
					F_QUQY_HTMQTY:res.F_QUQY_HTMQTY,
					F_QUQY_SCMQTY:res.F_QUQY_SCMQTY,
					F_QUQY_COLUMNQTY: res.F_QUQY_COLUMNQTY,
					F_QUQY_HTMPRICE: res.F_QUQY_HTMPRICE,
					F_QUQY_HSPRICE: res.F_QUQY_HSPRICE,
					F_QUQY_PHONE: res.F_QUQY_PHONE,
					F_QUQY_YZTYPE: res.F_QUQY_YZTYPE,
					F_QUQY_SLPRICE:res.F_QUQY_SLPRICE,
					TypeName: state.yzType[res.F_QUQY_YZTYPE],
					F_QUQY_MILEAGE: res.F_QUQY_MILEAGE,
					F_QUQY_LOSSQTY: res.F_QUQY_LOSSQTY,
					F_QUQY_YWY: {
						FSTAFFNUMBER: res.F_QUQY_YWY?.FStaffNumber
					},
					F_QUQY_YWYName: res.F_QUQY_YWY?.Name[0].Value,
					F_QUQY_LIAOCAR: {
						FNumber: res.F_QUQY_LIAOCAR?.Number
					},
					F_QUQY_LIAOCARName: res.F_QUQY_LIAOCAR?.Name[0].Value,
					F_QUQY_LIAOCARTEM: res.F_QUQY_LIAOCAR?.F_QUQY_CarNumber,
					F_QUQY_ADDRESS: res.F_QUQY_ADDRESS,
					F_QUQY_ORGCUST: {
						FNumber: res.F_QUQY_ORGCUST?.Number
					},
					F_QUQY_ORGCUSTName: res.F_QUQY_ORGCUST?.Name[0].Value,
					F_QUQY_REMARK: res.F_QUQY_REMARK,
					DocumentStatus: res.DocumentStatus,
					F_QUQY_FJID: res.F_QUQY_FJID
				}
				// 🍕 财务部分
				let obj2 = {
					F_QUQY_ABATTOIR: {
						FNumber: res.F_QUQY_ABATTOIR?.Number
					},
					F_QUQY_ABATTOIRName: res.F_QUQY_ABATTOIR?.Name[0].Value,
					F_QUQY_MIAOPRICE: res.F_QUQY_MIAOPRICE,
					F_QUQY_BZJPRICE: res.F_QUQY_BZJPRICE,
					F_QUQY_FXPRICE: res.F_QUQY_FXPRICE,
					F_QUQY_SYPRICE: res.F_QUQY_SYPRICE,
					// F_QUQY_SLPRICE: res.F_QUQY_SLPRICE,
					F_QUQY_STANDARD: res.F_QUQY_STANDARD,
					F_QUQY_SUPPLIER: {
						FNumber: res.F_QUQY_SUPPLIER?.Number
					},
					F_QUQY_SUPPLIERName: res.F_QUQY_SUPPLIER?.Name[0].Value,
					F_QUQY_MIAOCAR: {
						FNumber: res.F_QUQY_MIAOCAR?.Number
					},
					F_QUQY_MIAOCARName: res.F_QUQY_MIAOCAR?.Name[0].Value,
					F_QUQY_MIAOCARTEM: res.F_QUQY_MIAOCAR?.F_QUQY_CarNumber,
					F_QUQY_JSY: {
						FSTAFFNUMBER: res.F_QUQY_JSY?.FStaffNumber
					},
					F_QUQY_JSYName: res.F_QUQY_JSY?.Name[0].Value,
					F_QUQY_JXS: {
						FNUMBER: res.F_QUQY_JXS?.Number
					},
					F_QUQY_JXTYPE: res.F_QUQY_JXTYPE,
					F_QUQY_JXSName: res.F_QUQY_JXS?.Name[0].Value,
					F_QUQY_TZBJ: res.F_QUQY_TZBJ,
					FEntity: res.QUQY_SeedPlanEntry.map(it => {
						return {
							F_QUQY_MATERIALName: it.F_QUQY_MATERIAL?.Name[0].Value,
							F_QUQY_HTPRICE: it.F_QUQY_HTPRICE
						}
					})
				}
				// 🍕 如果没有屠宰场信息就置空财务信息；此时为业务员编辑单据
				if (!obj2.F_QUQY_ABATTOIRName) obj2 = {}
				Object.assign(state.info.Data.Model, obj, obj2)
				state.loading = false
				getFiles(Id)
			}
		})
	}
	const upload = ref(null)
	const getFiles = async (Id) => {
		console.log()
		let fjs = state.info.Data.Model.F_QUQY_FJID.split(',').filter(it => it.trim())
		if (fjs.length == 0) return
		let files = []
		let files2 = []
		for (let it of fjs) {
			let fileInfo = await kdApis.formView({ formId: 'BOS_Attachment', data: { Id: it } })
			if (fileInfo) {
				let fileDetais = await kdApis.fileDownLoad({ data: { FileId: fileInfo.FileId } })
				if (fileDetais) {
					files.push({ path: fileDetais.Result.FilePart, FileId: fileInfo.FileId, Id: it, FieldName: fileDetais.Result.FileName })
					files2.push({ path: 'data:image/' + fileInfo.ExtName.slice(1) + ';base64,' + fileDetais.Result.FilePart, FileId: fileInfo.FileId, Id: it, FieldName: fileDetais.Result.FileName })
				}
			}
		}
		upload.value.data.fileList2 = files2
		upload.value.data.fileList = files
	}

	onLoad(async (query) => {
		if (query.Id) {
			state.pageName = "上苗计划详情"
			getDetails(query.Id)
		}
		state.myInfo = uni.getStorageSync('myInfo')
		// 财务的话合并参数
		if (state.myInfo.role.includes(7)) Object.assign(state.info.Data.Model, state.Model2)
	})
</script>
<style lang="scss" scoped>
	.card {
		position: relative;
		width: calc(100% - 60rpx);
		margin: 20rpx auto;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
		border-radius: 6rpx;

		.card-index {
			position: absolute;
			display: flex;
			width: 40rpx;
			height: 40rpx;
			padding-left: 10rpx;
			font-size: 24rpx;
			background: #409eff;
			color: #fff;
			clip-path: polygon(0 0, 0 40rpx, 10rpx 40rpx, 40rpx 10rpx, 40rpx 0);
		}
	}
</style>